﻿using AutoMapper;
using MediatR;
using ZGLXM.Domain;
using ZGLXM.ErrorCode;
using ZGLXM.Infra.Interface;
using ZGLXM.Writh.Api.Applicantion.Command.RBAC;

namespace ZGLXM.Writh.Api.Applicantion.Handler.RBAC
{
    public class UserRoleInsertCommandHandler : IRequestHandler<UserRoleInsertCommand, ApiResult>
    {
        private readonly IUserRepository userRepository;
        private readonly IMapper mapper;
        private readonly ILogger<UserRoleInsertCommandHandler> logger;

        public UserRoleInsertCommandHandler(IUserRepository userRepository, IMapper mapper, ILogger<UserRoleInsertCommandHandler> logger)
        {
            this.userRepository = userRepository;
            this.mapper = mapper;
            this.logger = logger;
        }

        public async Task<ApiResult> Handle(UserRoleInsertCommand request, CancellationToken cancellationToken)
        {
            var user = mapper.Map<User>(request);
            var result = await userRepository.UserRoleInsert(user, request.RoleId);
            if (result)
            {
                return ApiResult.OK("新增用户角色成功");
            }
            return ApiResult.Fail("新增用户角色失败");
        }
    }
}
